<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3 0003
 * Time: 15:24
 */
$http = new swoole_http_server('192.168.3.221', 8811);

//设置静态文件直接访问
$http->set([
    'worker_num' => 4,
    'enable_static_handler' => true,
    'document_root' => '/data/wwwroot/www/swoole-demo/mooc/tp5/public/static',
]);

//进程启动时发生,这里创建的对象可以在进程生命周期内使用
$http->on('WorkerStart', function($server, $worker_id){
    //热加载:加载框架里面的文件,加载入口文件的内容，不直接启动
    require __DIR__ . '/../thinkphp/base.php';
    //require __DIR__ . '/../public/index.php';
});

$http->on('request', function($request, $response) use($http){
    //require_once __DIR__ . '/../thinkphp/base.php';
    //转换数据获取方式，将swoole的获取数据方式$request->get转换为$_REQUEST['get'],post,server一样
    //if(!empty($_GET)) unset($_GET);     //注销标量，$http->close(); 注销worker中的变量，释放内存
    $_GET = [];
    $_POST = [];
    //$_SERVER = [];
    if(isset($request->get)){
        foreach ($request->get as $k => $v){
            $_GET[$k] = $v;
        }
    }
    if(isset($request->post)){
        foreach ($request->post as $k => $v){
            $_POST[$k] = $v;
        }
    }
    if(isset($request->server)){
        foreach ($request->server as $k => $v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    ob_start();
    // 执行应用并响应
    try{
        //run() 方法在thinkphp/library/think/app.php
        think\Container::get('app')->run()->send();
    }catch(\Exception $e){
        //todo
    }
    //echo '-action-'.request()->action().PHP_EOL;
    $res = ob_get_contents();
    ob_end_clean();
    $response->header("Content-Type", "text/html;charset=utf-8");
    $response->end($res);
    //常驻内存释放
    //$http->close();   //注销worker的变量,释放内存
    //通过执行应用并响应的run()方法，改变接受路劲的代码
    //run()方法在 thinkphp/library/think/app.php
    //run()->routeCheck()->path()->pathinfo()   检测路由
});

$http->start();











